import React, { Component } from 'react';
import {nanoid} from 'nanoid';

export default class Header extends Component {
  addInfo = (e) => {
    const {addTodo} = this.props
    const {target,keyCode} = e
    if (keyCode !== 13) return;
    if (target.value.trim() === ''){
      alert('不能为空')
      return false
    }
    const todoObj = {
      id: nanoid(),
      name: target.value,
      done: false
    }
    addTodo(todoObj)
  };
  render() {
    return (
      <div className='todo-header'>
        <input
          type='text'
          placeholder='请输入你的任务名称，按回车键确认'
          onKeyDown={this.addInfo}
        />
      </div>
    );
  }
}
